package com.example.xuanmei.user.service;

import com.example.xuanmei.common.dto.ApiResponse;
import com.example.xuanmei.user.dto.LoginRequest;
import com.example.xuanmei.user.dto.RegisterRequest;
import com.example.xuanmei.user.dto.UserDto;
import com.example.xuanmei.user.entity.User;
import com.example.xuanmei.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户服务
 */
@Service
@RequiredArgsConstructor
public class UserService {
    private final UserRepository userRepository;
    private final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
    
    /**
     * 用户注册
     */
    public ApiResponse<Map<String, Object>> register(RegisterRequest request) {
        if (userRepository.existsByUsername(request.getUsername())) {
            return ApiResponse.error("用户名已存在");
        }
        
        User user = new User();
        user.setUsername(request.getUsername());
        user.setPassword(passwordEncoder.encode(request.getPassword()));
        user.setNickname(request.getNickname());
        user.setFollowers(0L);
        user.setFollowing(0L);
        user.setLikes(0L);
        
        User savedUser = userRepository.save(user);
        
        Map<String, Object> result = new HashMap<>();
        result.put("userId", savedUser.getId());
        result.put("username", savedUser.getUsername());
        result.put("token", "mock-jwt-token-" + savedUser.getId());
        
        return ApiResponse.success("注册成功", result);
    }
    
    /**
     * 用户登录
     */
    public ApiResponse<Map<String, Object>> login(LoginRequest request) {
        User user = userRepository.findByUsername(request.getUsername())
                .orElseThrow(() -> new RuntimeException("用户名或密码错误"));
        
        if (!passwordEncoder.matches(request.getPassword(), user.getPassword())) {
            return ApiResponse.error("用户名或密码错误");
        }
        
        Map<String, Object> result = new HashMap<>();
        result.put("userId", user.getId());
        result.put("username", user.getUsername());
        result.put("token", "mock-jwt-token-" + user.getId());
        
        return ApiResponse.success("登录成功", result);
    }
    
    /**
     * 获取用户信息
     */
    public ApiResponse<UserDto> getUserInfo(Long userId) {
        User user = userRepository.findById(userId)
                .orElseThrow(() -> new RuntimeException("用户不存在"));
        
        UserDto dto = convertToDto(user);
        return ApiResponse.success(dto);
    }
    
    private UserDto convertToDto(User user) {
        UserDto dto = new UserDto();
        dto.setId(user.getId());
        dto.setUsername(user.getUsername());
        dto.setNickname(user.getNickname());
        dto.setAvatar(user.getAvatar());
        dto.setDescription(user.getDescription());
        dto.setFollowers(user.getFollowers());
        dto.setFollowing(user.getFollowing());
        dto.setLikes(user.getLikes());
        dto.setCreateTime(user.getCreateTime());
        return dto;
    }
}
